package ead.cs.service.workflow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public abstract class AbstractStateMachine<S> implements StateMachine<S> {

    @Autowired(required = false)
    List<StatusExecutor<S>> statusExecutorList;

    Map<S, StatusExecutor<S>> statusExecutorRegistry = new HashMap<S, StatusExecutor<S>>();

    @PostConstruct
    public void setup() {
        if(CollectionUtils.isEmpty(statusExecutorList)){
            return;
        }
        for(StatusExecutor<S> statusExecutor : statusExecutorList){
            statusExecutorRegistry.put(statusExecutor.getStatus(),statusExecutor);
        }
    }

    @Override
    public StatusExecutor getExecutor(S status) {
        return statusExecutorRegistry.get(status);
    }
}
